﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WBFSe3.IO;
using System.IO;
using System.Threading;
using WBFSe3.Wbfs;
using WBFSe3.Streams;
using WBFSe3.Interfaces;

namespace WBFSe3.Formats
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------


    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public abstract class WbfsFormatReader
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private int activeStreams = 0;

        protected int version = 0;
        protected int sectorSize = 0;
        protected long fileLength = 0;
        protected long fileSize = 0;
        protected WbfsPath path = null;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public int ActiveStreams { get { return this.activeStreams; } }
        public int Version { get { return this.version; } }
        public int SectorSize { get { return this.sectorSize; } }
        public long FileLength { get { return this.fileLength; } }
        public long FileSize { get { return this.fileSize; } }
        public WbfsPath Path { get { return this.path; } }

        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public abstract WbfsError Initialize(WbfsDriveInfo drive, WbfsPath directory, String file);


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public abstract WbfsError Fix();


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public abstract WbfsError ChangeHeader(String name, String code, WbfsRegion region);


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsError GetStream(FileAccess access, FileShare share, out WbfsStream stream)
        {
            WbfsError error;
            WbfsStream wstream;

            if ((error = EmmitStream(access, share, out wstream)) != WbfsError.Ok)
            {
                stream = null;
                return error;
            }

            Interlocked.Increment(ref this.activeStreams);
            wstream.StreamClosed += new StreamClosedDelegate(StreamClosed);
            stream = wstream;

            return WbfsError.Ok;
        }


        //---------------------------------------------------------------------------------------------------
        // 
        //---------------------------------------------------------------------------------------------------
        protected abstract WbfsError EmmitStream(FileAccess access, FileShare share, out WbfsStream stream);


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private void StreamClosed(Stream sender)
        {
            Interlocked.Decrement(ref this.activeStreams);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public abstract void Close();
    }
}
